<script setup lang="ts">
  import { defineProps, ref, watch } from 'vue';
  import { useBooleanStore } from '@/store';
  import { Notification } from '@arco-design/web-vue';
  import { delMsLargeVehicle } from '@/api/les';

  const props = defineProps<{
    tableData: any;
  }>();
  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'index',
      slotName: 'index',
      align: 'center',
    },
    {
      title: '车辆编码',
      dataIndex: 'largeVehicleCode',
    },
    {
      title: '创建人',
      dataIndex: 'createUser',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '修改人',
      dataIndex: 'updateUser',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '修改时间',
      dataIndex: 'updateTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '操作',
      dataIndex: 'action',
      slotName: 'action',
      fixed: 'right',
      align: 'center',
    },
  ];

  const booleanStore = useBooleanStore();
  // 创建一个响应式的本地副本
  const localTableData = ref<any[]>([]);
  // 数据库一共有多少条
  const total = ref<number>(0);
  const current = ref<number>(1);
  const size = ref<number>(10);

  // 监听props.tableData的变化，并更新本地副本
  watch(
    () => props.tableData,
    () => {
      localTableData.value = props.tableData?.responseBody?.records || [];
      total.value = props.tableData?.responseBody?.total || 0;
    },
    { immediate: true }
  );

  function change(e: any) {
    current.value = e;
  }
  function changeSize(e: any) {
    size.value = e;
  }
  function onEdit(record: any) {
    booleanStore.setRow(record);
    booleanStore.toggleBool(114);
  }
  // 删除
  async function onDel(record: any) {
    const data = await delMsLargeVehicle({
      msLargeVehicleId: record.msLargeVehicleId,
    });
    if (data.retCode === '200') {
      Notification.success({
        title: '成功',
        content: data.message,
        closable: true,
      });
    } else {
      Notification.error({
        title: '失败',
        content: data.message,
        closable: true,
      });
    }
    booleanStore.watchUpdate(114);
  }
  // 监听页面变化
  watch([current, size], () => {
    booleanStore.setPage(current.value, size.value, 112);
  });
</script>

<template>
  <div>
    <a-table
      :stripe="true"
      class="top-table"
      :pagination="false"
      :columns="columns"
      :data="localTableData"
    >
      <template #index="{ record }">{{
        localTableData.indexOf(record) + 1
      }}</template>
      <template #action="{ record }">
        <a-button type="text" class="font-color" @click="onEdit(record)"
          >修改</a-button
        >
        <a-popconfirm
          content="你确定要删除吗?"
          ok-text="确认"
          cancel-text="取消"
          @ok="onDel(record)"
        >
          <a-button type="text" class="font-color">删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>
    <div class="page-position">
      <a-pagination
        :total="total"
        :current="current"
        :page-size="size"
        show-total
        show-jumper
        show-page-size
        @change="change"
        @update:pageSize="changeSize"
      />
    </div>
  </div>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
